package cae.gui.destination;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cae.gui.util.CaeAlert;
import cae.gui.util.CaeJDialog;
import cae.logic.destination.CaeDestination;
import cae.logic.destination.CaeDestinationException;
import cae.logic.destination.CaeDestinatiosManager;
import cae.logic.destination.CaeGroup;
import cae.logic.destination.CaeGroupManager;


public class CaeAddGroupDialog extends CaeJDialog implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel panel = null;
	private DualListBox<String> dual;
	
	private JTextField nick;
	
	public CaeAddGroupDialog(JFrame frame, boolean modal)
	{
		
		super(frame, "Agregar Grupo: ", modal);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		panel = new JPanel();
		getContentPane().add(panel);
		
		panel.setLayout( new GridBagLayout() );
		GridBagConstraints destConst = new GridBagConstraints();
		destConst.insets = new Insets(5,5,5,5);
		destConst.anchor = GridBagConstraints.WEST;
		destConst.fill = GridBagConstraints.HORIZONTAL;
		
		destConst.gridx = 0;
		destConst.gridy = 0;
		panel.add(new JLabel("Nick: "), destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 0;
		nick = new JTextField();
		nick.setMinimumSize(new Dimension(200, 22));
		nick.setMaximumSize(new Dimension(200, 22));
		nick.setPreferredSize(new Dimension(200, 22));
		panel.add(nick, destConst);
		
		dual = new DualListBox<String>();
		dual.setDestinationChoicesTitle("Miembros");
		dual.setSourceChoicesTitle("Contactos");
		
		List<CaeDestination> dests = CaeDestinatiosManager.getInstance().getDestinations();
		List<String> values = new ArrayList<String>();
		for (CaeDestination caeDestination : dests) {
			values.add(caeDestination.getAlias()); // TODO mmmm
		}
	    dual.addSourceElements(values);
	    destConst.gridx = 0;
		destConst.gridy = 1;
		destConst.gridwidth = 2;
		panel.add(dual, destConst);
	    
	    
		
		destConst.gridwidth = 1;
		destConst.gridx = 1;
		destConst.gridy = 3;
		JButton add = new JButton("Agregar");
		add.addActionListener(this);
		panel.add(add, destConst);
		
		pack();
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width / 4, screenSize.height /4);
        
		setResizable(false);
		setAlwaysOnTop(true);
		setVisible(true);
		
		// TODO FOCO
		//FocusManager.getCurrentManager().
		
	}

	public void actionPerformed(ActionEvent e)
	{
		String nickS = nick.getText();
		
		// SI NO INGRESO ALGUN DATO
		if( nickS.isEmpty())
		{
			CaeAlert.error("Ingrese un nick", this);
			return;
		}
		
		// NICK NO REPETIDO
		if( CaeGroupManager.getInstance().getGroup(nickS) != null)
		{
			CaeAlert.error("El nick ya esta en uso", this);
			return;
		}

		//AL MENOS UNA SELECCION
		if (!dual.destinationIterator().hasNext() )
		{
			CaeAlert.error("Seleccione al menos un miembro", this);
			return;
		}
		
		CaeGroup group = new CaeGroup(nickS);
		try {
			Iterator<String> it = dual.destinationIterator();
			while( it.hasNext())
			{
				String alias = (String) it.next();
				CaeDestination d = CaeDestinatiosManager.getInstance().getDestinationByAlias(alias);
				group.getMembers().add(d.getAlias());
			}
			CaeGroupManager.getInstance().addGroup(group);
		} catch (CaeDestinationException e1) {
			CaeAlert.error("La maquina ya tiene asociado un grupo con ese nick", this);
			return;
		}
		
		setVisible(false);
		
	}
}
